package com.info.dpunified.defects;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.info.dpunified.common.dataaccess.DPUStaticDataDAO;
import com.info.dpunified.defects.dataaccess.dao.DefectsSelectorDAO;
import com.info.dpunified.defects.transfer.DefectsSelection;

public class DefectsSubmissionStatusSelector {

	/**
	 * This DAO class fetches the filter criteria needed by the user to view
	 * defects submitted
	 */
	private DefectsSelectorDAO defectsSelectorDAO;

	/**
	 * This DAO class fetches the static data
	 */
	private DPUStaticDataDAO dpuStaticDataDAO;
	
	
	/**
	 * @return the defectsSelectorDAO
	 */
	public DefectsSelectorDAO getDefectsSelectorDAO() {
		return defectsSelectorDAO;
	}


	/**
	 * @param defectsSelectorDAO the defectsSelectorDAO to set
	 */
	public void setDefectsSelectorDAO(DefectsSelectorDAO defectsSelectorDAO) {
		this.defectsSelectorDAO = defectsSelectorDAO;
	}


	/**
	 * @return the dpuStaticDataDAO
	 */
	public DPUStaticDataDAO getDpuStaticDataDAO() {
		return dpuStaticDataDAO;
	}


	/**
	 * @param dpuStaticDataDAO the dpuStaticDataDAO to set
	 */
	public void setDpuStaticDataDAO(DPUStaticDataDAO dpuStaticDataDAO) {
		this.dpuStaticDataDAO = dpuStaticDataDAO;
	}


	public DefectsSubmissionStatusSelector()
	{
		XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-context.xml")) ;
		defectsSelectorDAO =(DefectsSelectorDAO) beanFactory.getBean("defectsSelectorDAO");
		dpuStaticDataDAO =(DPUStaticDataDAO) beanFactory.getBean("dpuStaticDataDAO");
	}
	
	public List<String> fetchDefectStatusYears()
	{
		return defectsSelectorDAO.fetchDefectsSubmittedYears();
	}
	
	public HashMap<String, ArrayList<String>> fetchSubmittedDefects(String year)
	{
		List<DefectsSelection> defects =  this.getDefectsSelectorDAO().fetchDefectsSubmissionStatus(year);
		
		HashMap<String, ArrayList<String>> defectStatusMap =  new HashMap<String, ArrayList<String>>();
		
		for(DefectsSelection defect : defects)
		{
			ArrayList<String> tempList = new ArrayList<String>();
			
			String key = defect.getMonth().toUpperCase();
			String value = defect.getProjectCode();
			
			if(defectStatusMap.containsKey(key))
			{
				tempList = defectStatusMap.get(key);
				tempList.add(value);
				defectStatusMap.put(key, tempList);
			}
			
			else
			{
				tempList.add(value);
				defectStatusMap.put(key, tempList);
			}
		}
		
		return defectStatusMap;
		
	}
	
	public List<String> fetchMonthsList()
	{
		return this.getDpuStaticDataDAO().fetchMonths();
	}
}
